package cn.xiaobai.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.xiaobai.enums.TimeTypeEnum;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理工具
 *
 * @Author yangdaji
 * @Date 2023/4/28 0028
 * @PackageName cn.xiaobai.service.test
 * @ClassName TimeUtils
 */
public class TimeUtils {

    private static String YYYY_MM_DD = "yyyy-MM-dd";

    private static String YYYYMMDD = "yyyyMMdd";

    private static String YYYY_M_D = "yyyy-M-d";

    /**
     * 根据当前日期获取年月日
     *
     * @param date 日期
     * @param type 类型
     * @return
     */
    public static int dateToYearMonthDay(Date date, String type) {

        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        Integer calendar = TimeTypeEnum.getItem(type);
        return instance.get(calendar);
    }

    /**
     * 根据当前时间获取周五的日期
     *
     * @param date
     * @return
     */
    public static Date fridayWeek(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        int week = instance.get(Calendar.DAY_OF_WEEK);
        // 判断当前周数,周日为1,以此类推, 所以如果从周一开始, 那么周五就是 +4
        // 周日
        int sunday = 1;
        // 周五
        int friday = 6;
        if (week == sunday) {
            //如果当前时间的周数是1,则是周日, 则周五的间隔需要加5
            return dateCalcul(date, 5);
        } else {
            // 否则只需要用周五减去当前周数即为当前时间到周五的间隔
            int interval = friday - week;
            if (interval < 0) {
                interval = sunday;
            }
            return dateCalcul(date, interval);
        }
    }

    public static Date fridayWeekSaturday(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        // 获取当前周数
        int week = instance.get(Calendar.DAY_OF_WEEK);
        // 判断当前周数,周日为1,以此类推, 所以如果从周一开始, 那么周五就是 +4
        // 周六
        int saturday = 7;
        int interval = saturday - week;

        // 如果当前是周六
        return dateCalcul(date, Math.max(interval, 0));
    }


    /**
     * 计算日期,根据间隔
     *
     * @param date     日期
     * @param interval 间隔, 负数: 计算过去时间, 正数: 计算未来时间
     * @return
     */
    public static Date dateCalcul(Date date, int interval) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.add(Calendar.DAY_OF_MONTH, interval);
        return instance.getTime();
    }

    /**
     * 获取当前年的第一天
     *
     * @return
     */
    public static Date getYearFirstDay() {
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        instance.set(Calendar.DAY_OF_YEAR, 1);
        return instance.getTime();
    }

    /**
     * 获取当前年的最后一天, 日期格式
     *
     * @return
     */
    public static Date getYearLastDay() {
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        instance.set(Calendar.DAY_OF_YEAR, instance.getActualMaximum(Calendar.DAY_OF_YEAR));
        return instance.getTime();
    }

    /**
     * 获取当前年的最后一天, 数字格式
     *
     * @return
     */
    public static int getYearLastInt() {
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        return  instance.getActualMaximum(Calendar.DAY_OF_YEAR);
    }

    /**
     * 获取当前月的第一天
     *
     * @return
     */
    public static Date getMonthFirstDay() {
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        instance.set(Calendar.DAY_OF_MONTH, 1);
        return instance.getTime();
    }

    /**
     * 获取当前月的最后一天, 日期格式
     *
     * @return
     */
    public static Date getMonthLastDay() {
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        int actualMaximum = instance.getActualMaximum(Calendar.DAY_OF_MONTH);
        instance.set(Calendar.DAY_OF_MONTH, actualMaximum);
        return instance.getTime();
    }

    /**
     * 获取当前月的最后一天, 数值格式
     *
     * @return
     */
    public static int getMonthLastInt() {
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        return instance.getActualMaximum(Calendar.DAY_OF_MONTH);
    }


    /**
     * 日期转换为字符串格式
     *
     * @param date 日期
     * @return 字符串格式日期
     */
    public static String dateToString(Date date) {
        return LocalDateTimeUtil.format(DateUtil.toLocalDateTime(date)
                , YYYY_MM_DD);
    }


    /**
     * 修正字符串格式日期, 如 2023-4-1 修正为 2023-04-01
     *
     * @param dateStr 日期
     * @return
     */
    public static String revisionDate(String dateStr) {
        String[] dateStrs = dateStr.split("-");
        String year = dateStrs[0];
        String month = dateStrs[1];
        String day = dateStrs[2];
        // 补全月日
        String leftPadMonth = StringUtils.leftPad(month, 2, '0');
        String leftPadDay = StringUtils.leftPad(day, 2, '0');
        // 重新组装
        return year + "-" + leftPadMonth + "-" + leftPadDay;
    }

    /**
     * 修正字符串格式日期, 如 2023-5-1 修正为 20230501
     *
     * @param timeStr 日期
     * @return
     */
    public static String revisionDateZone(String timeStr) {
        DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern(YYYY_M_D);
        DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern(YYYYMMDD);
        return LocalDate.parse(timeStr, inputFormat).format(outputFormat);
    }

}
